﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ajayumi.develop.win32.Transport.Utils
{
    /// <summary>
    /// SerializerHelper.cs 序列化辅助类
    /// Author   : 黄乙冬
    /// Date     : 2011-12-02
    /// </summary>
    public class SerializerHelper
    {

        /// <summary>
        /// 将对象序列化成二进制流
        /// </summary>
        /// <param name="request">待序列化的对象</param>
        /// <returns>返回序列化后的二进制流</returns>
        public static MemoryStream SerializeBinary(object request)
        {
            BinaryFormatter serializer = new BinaryFormatter();
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            serializer.Serialize(memStream, request);
            return memStream;
        }

        /// <summary>
        /// 二进制流反序列化为对象
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static object DeSerializeFromBinary(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                ms.Seek(0, SeekOrigin.Begin);
                BinaryFormatter deserializer = new BinaryFormatter();
                object newobj = deserializer.Deserialize(ms);
                return newobj;
            }
        }

        /// <summary>
        /// 序列化对象为XML
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static MemoryStream SerializeXml<T>(T model)
        {
            XmlSerializer _XmlSerializer = new XmlSerializer(typeof(T));
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            _XmlSerializer.Serialize(memStream, model);
            return memStream;
        }

        /// <summary>
        /// 序列化对象为XML
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static void SerializeXml<T>(T model, string path)
        {
            using (MemoryStream _MemoryStream = SerializeXml<T>(model))
            {
                byte[] _Byte = _MemoryStream.ToArray();
                using (FileStream f = new FileStream(path, FileMode.OpenOrCreate))
                {
                    f.Write(_Byte, 0, _Byte.Length);
                }

            }
        }

        /// <summary>
        /// XML反序列化为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T DeSerializeFromXml<T>(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                ms.Seek(0, SeekOrigin.Begin);
                XmlSerializer _XmlSerializer = new XmlSerializer(typeof(T));
                T _Obj = (T)_XmlSerializer.Deserialize(ms);
                return _Obj;
            }
        }


    }
}
